package DataBase;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.example.drink_order_system.entity.Order;
import com.example.drink_order_system.R;
import com.example.drink_order_system.entity.discount_quan;
import com.example.drink_order_system.tool.ImageUtil;

import java.util.ArrayList;

public class UserDbHelper extends SQLiteOpenHelper {
    private static final int VERSION = 1;
    private static final String DbName = "AndroidAPP.db";
    //单例模式
    private static SQLiteOpenHelper mySqliteHelper;
    private Context myContext;
    private UserDbHelper(Context context,String Name,SQLiteDatabase.CursorFactory factory, int version) {
        super(context, Name,factory,version);
        myContext = context;
    }
    public static SQLiteOpenHelper getSqliteHelper(Context context){
        if (mySqliteHelper == null){
            mySqliteHelper = new UserDbHelper(context,DbName,null,VERSION);
        }
        return mySqliteHelper;
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table Users(_id integer primary key autoincrement,UserName varchar(15) unique,PassWord varchar(20),Score integer,User_img Text,discount_num integer)");
        db.execSQL("create table bill(_id integer primary key autoincrement,UserName varchar(15),OrderNum varchar(25),dateTime varchar(25),ifTakeAway varchar(10),cost varchar(10))");
        db.execSQL("create table discount(_id integer primary key autoincrement,tag integer,UserName varchar(15),detail Text,start_time Text,end_time Text,score Integer)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    public void register(String name,String password){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues cv = new ContentValues();
        Bitmap decodeResource = BitmapFactory.decodeResource(myContext.getResources(),R.drawable.head_photo);
        String userImg = ImageUtil.imageToBase64(decodeResource);
        cv.put("UserName",name);
        cv.put("PassWord",password);
        cv.put("Score",0);
        cv.put("User_img",userImg);
        cv.put("discount_num",0);
        db.insert("Users",null,cv);
        db.close();
    }
    public boolean login(String username, String password){
        SQLiteDatabase db = getWritableDatabase();
        Cursor user = db.query("Users",null,"UserName like?",new String[]{username},null,null,null);
        if (user!=null){
            while (user.moveToNext()){
                @SuppressLint("Range") String username1 = user.getString(user.getColumnIndex("UserName"));
                @SuppressLint("Range") String password1 = user.getString(user.getColumnIndex("PassWord"));
                if (username1.equals(username)&&password1.equals(password)){
                    user.close();
                    db.close();
                    return true;
                }
            }
        }
        if (user!=null){
            user.close();
        }
        db.close();
        return false;
    }
    public int getScore(String username){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("select Score from Users where UserName=?",new String[]{username});
        cursor.moveToFirst();
        @SuppressLint("Range") int score = cursor.getInt(cursor.getColumnIndex("Score"));
        cursor.close();
        db.close();
        return score;
    }
    public String getImg(String username){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("select User_img from Users where UserName=?",new String[]{username});
        cursor.moveToFirst();
        @SuppressLint("Range") String img = cursor.getString(cursor.getColumnIndex("User_img"));
        cursor.close();
        db.close();
        return img;
    }
    public String get_dis_num(String username){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("select discount_num from Users where UserName=?",new String[]{username});
        cursor.moveToFirst();
        @SuppressLint("Range") String dis_num = cursor.getString(cursor.getColumnIndex("discount_num"));
        cursor.close();
        db.close();
        return dis_num;
    }
    public void save_dis_num(String username,int num){
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("update Users set discount_num='"+num+"' where UserName='"+username+"'");
        db.close();
    }
    public void saveScore(String username,int score){
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("update Users set Score='"+score+"' where UserName='"+username+"'");
        db.close();
    }
    public void change_info(String last_name,String username,String password,String img_source){
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("update Users set UserName='"+username+"',Password='"+password+"',User_img='"+img_source+"' where UserName='"+last_name+"'");
        db.execSQL("update discount set UserName='"+username+"' where UserName='"+last_name+"'");
        db.close();
    }
    public void reset_info(String name,String pass){
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("update Users set Password='"+pass+"' where UserName='"+name+"'");
        db.close();
    }
    public void saveDiscount(String username,discount_quan dis){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("UserName",username);
        cv.put("detail",dis.getDetail());
        cv.put("start_time",dis.getStart_date());
        cv.put("end_time",dis.getEnd_date());
        cv.put("score",dis.getScore());
        cv.put("tag",dis.getTag());
        db.insert("discount",null,cv);
        db.close();
    }
    public ArrayList<discount_quan> readDiscount(String username){
        ArrayList<discount_quan> dis = new ArrayList<>();
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("select detail,start_time,end_time,score,tag from discount where UserName=?",new String[]{username});
        while (cursor.moveToNext()){
            @SuppressLint("Range") String detail = cursor.getString(cursor.getColumnIndex("detail"));
            @SuppressLint("Range") String start_time = cursor.getString(cursor.getColumnIndex("start_time"));
            @SuppressLint("Range") String end_time = cursor.getString(cursor.getColumnIndex("end_time"));
            @SuppressLint("Range") int score = cursor.getInt(cursor.getColumnIndex("score"));
            @SuppressLint("Range") int tag = cursor.getInt(cursor.getColumnIndex("tag"));
            dis.add(new discount_quan(detail,score,start_time,end_time,tag));
        }
        cursor.close();
        db.close();
        return dis;
    }
    public void delete_discount(int i){
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("delete from discount where tag='"+i+"'");
        db.close();
    }
    public void saveBill(String userName,String orderNum,String dateTime,String ifTake,String cost){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("UserName",userName);
        cv.put("OrderNum",orderNum);
        cv.put("dateTime",dateTime);
        cv.put("ifTakeAway",ifTake);
        cv.put("cost",cost);
        db.insert("bill",null,cv);
        db.close();
    }
    public ArrayList<Order> readBill(){
        ArrayList<Order> orders = new ArrayList<>();
        SQLiteDatabase db = getReadableDatabase();
        Cursor billCursor = db.rawQuery("select OrderNum,dateTime,ifTakeAway,cost from bill",null);
        if (billCursor!=null){
            while (billCursor.moveToNext()){
                @SuppressLint("Range") String OrderNum = billCursor.getString(billCursor.getColumnIndex("OrderNum"));
                @SuppressLint("Range") String dateTime = billCursor.getString(billCursor.getColumnIndex("dateTime"));
                @SuppressLint("Range") String takeAway = billCursor.getString(billCursor.getColumnIndex("ifTakeAway"));
                @SuppressLint("Range") String cost = billCursor.getString(billCursor.getColumnIndex("cost"));
                Order order = new Order();
                order.setOrder_number(OrderNum);
                order.setTime(dateTime);
                order.setTakeAway(takeAway);
                order.setCost(cost);
                orders.add(order);
            }
        }
        billCursor.close();
        db.close();
        return orders;
    }
}
